﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace AutoStar.Common
{
    public class Encryption
    {
        private RSACryptoServiceProvider _rsa;
        public string publicKey = "<RSAKeyValue><Modulus>tUBwDVlUCdpl4bs0PeXt0npw2/UPBYsvs1mOmyJhrJb0xHLrtMmd1vw/xVccFGVp8zzMd5/IBLAekACJFSbzTKwodRKdezCXSjp7r32kmXVF81JeKmfT0aKFNc7+ePdE/LrcvyS8do4ATu5vxA3q57+LohMgDAs2ee7rIwVaMik=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>";
        public string privateKey = "<RSAKeyValue><Modulus>tUBwDVlUCdpl4bs0PeXt0npw2/UPBYsvs1mOmyJhrJb0xHLrtMmd1vw/xVccFGVp8zzMd5/IBLAekACJFSbzTKwodRKdezCXSjp7r32kmXVF81JeKmfT0aKFNc7+ePdE/LrcvyS8do4ATu5vxA3q57+LohMgDAs2ee7rIwVaMik=</Modulus><Exponent>AQAB</Exponent><P>xWGJuHJ1sJd5xY09Rbu18ojnW2j4aSoR/mo9DsfHI+Np4a606rM54XHhXG+YA7jheLKN4/90McgvvWuQLC5Mjw==</P><Q>6xSfimDs8I2cFwCwOiJ54ZtfCukCsbu6WunLgMdK1GdxjHWWDfpPnWugapXW2POX71Zcj4VP0h4zJgdLYj3hxw==</Q><DP>CwIIkz/W7nVUNj9NRoFUgWU/Oerdx8ev3zd/lj5wDcFWG18J+fjq5nbacbRLjxGViYyzux55FXmrp+3CyaP/Yw==</DP><DQ>6P8b0c+XiCUTHEl2YWhiXRXd8bZZEUf7wLgRF6ZvqG6OwKSbAWejGWffK0UwGP/3CyURwL5lCuEb2QlJ0il7aQ==</DQ><InverseQ>bZUIm31ilcA8GgkZLLTBbAXrP6KACLUd63BYL97IeHMGF7mbRTaZ23kjP8SCcnh/qxJGTRZKMn5wvvy+o/QV/A==</InverseQ><D>SO7D5+sw8MILyvgsPlf+CXqxXSDPyZhHOnDn/C6qhOs9VKs1g0q5k59u/bZemypvpqupTt1he9lugt1kS7mfnmWHShAzrwxRlRbSrAO0yh5HUbCt+n6HLGNWQXHMHvKsraeUdVKhMuyrQUgB9oPZLkdSVxcHQcWHia5Y2NDXV30=</D></RSAKeyValue>";

        #region 秘钥写入本地文件

        //private const string PublicKeyFile = "publicKey.xml";
        //private const string PrivateKeyFile = "privateKey.xml";
        //public Encryption()
        //{
        //    _rsa = LoadKeys();
        //    if (_rsa == null)
        //    {
        //        _rsa = new RSACryptoServiceProvider();
        //        SaveKeys(_rsa);
        //    }
        //}
        //private RSACryptoServiceProvider LoadKeys()
        //{
        //    if (File.Exists(PublicKeyFile) && File.Exists(PrivateKeyFile))
        //    {
        //        RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
        //        rsa.FromXmlString(File.ReadAllText(PublicKeyFile));
        //        rsa.FromXmlString(File.ReadAllText(PrivateKeyFile));
        //        return rsa;
        //    }
        //    return null;
        //}
        //private void SaveKeys(RSACryptoServiceProvider rsa)
        //{
        //    File.WriteAllText(PublicKeyFile, rsa.ToXmlString(false));
        //    File.WriteAllText(PrivateKeyFile, rsa.ToXmlString(true));
        //} 
        #endregion
        public Encryption()
        {
            _rsa = new RSACryptoServiceProvider();
            _rsa.FromXmlString(privateKey);
        }
        public string GetPublicKey()
        {
            return _rsa.ToXmlString(false);
        }

        public string GetPrivateKey()
        {
            return _rsa.ToXmlString(true);
        }

        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="plainText"></param>
        /// <returns></returns>
        public string Encrypt(string plainText)
        {
            _rsa.FromXmlString(publicKey);
            byte[] plainBytes = Encoding.UTF8.GetBytes(plainText);
            byte[] encryptedBytes = _rsa.Encrypt(plainBytes, false);
            return Convert.ToBase64String(encryptedBytes);
        }
        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="encryptedText"></param>
        /// <returns></returns>
        public string Decrypt(string encryptedText)
        {
            _rsa.FromXmlString(privateKey);
            byte[] encryptedBytes = Convert.FromBase64String(encryptedText);
            byte[] decryptedBytes = _rsa.Decrypt(encryptedBytes, false);
            return Encoding.UTF8.GetString(decryptedBytes);
        }
    }
}
